//
//  ByCellShopsIndexShopsItem.m
//  chequanbao_ios
//
//  Created by pro on 17/5/9.
//  Copyright © 2017年 com.itboye. All rights reserved.
//

#import "ByCellShopsIndexShopsItem.h"

#import "ByViewStarMark.h"

@implementation ByCellShopsIndexShopsItem
{
    UIView          *_bgView;
    UIImageView     *_iconView;
    UILabel         *_nameLabel;
    ByViewStarMark  *_starView;
    UILabel         *_sellLabel;
    UILabel         *_moneyLabel;
    UIImageView     *_addressIcon;
    UILabel         *_distanceLabel;
    
    UILabel         *_discountLabel;

}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        self.backgroundColor = [UIColor clearColor];
        [self setupUI];
        
        
    }
    return self;
}
- (void)setupUI {
    
    _bgView = [[UIView alloc] init];
    _bgView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:_bgView];
    
    _iconView = [[UIImageView alloc] init];
    [_bgView addSubview:_iconView];
    
    _nameLabel = [[UILabel alloc] init];
//    _nameLabel.adjustsFontSizeToFitWidth = YES;
    _nameLabel.font = FONT(16);
    _nameLabel.textColor = [UIColor blackColor];
    _nameLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_nameLabel];
    
    _starView = [[ByViewStarMark alloc] init];
    [_bgView addSubview:_starView];
    
    _sellLabel = [[UILabel alloc] init];
    _sellLabel.adjustsFontSizeToFitWidth = YES;
    _sellLabel.font = FONT(14);
    _sellLabel.textColor = [UIColor blackColor];
    _sellLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_sellLabel];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.adjustsFontSizeToFitWidth = YES;
    _moneyLabel.font = FONT(14);
    _moneyLabel.textColor = [UIColor blackColor];
    _moneyLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_moneyLabel];
    
    _addressIcon = [[UIImageView alloc] init];
    _addressIcon.image = [UIImage imageNamed:@"Shops_address_icon"];
    [_bgView addSubview:_addressIcon];
    
    _distanceLabel = [[UILabel alloc] init];
    _distanceLabel.adjustsFontSizeToFitWidth = YES;
    _distanceLabel.font = FONT(14);
    _distanceLabel.textColor = [UIColor lightGrayColor];
    _distanceLabel.textAlignment = NSTextAlignmentLeft;
    [_bgView addSubview:_distanceLabel];
    
    _discountLabel = [[UILabel alloc] init];
    _discountLabel.textAlignment = NSTextAlignmentRight;
    _discountLabel.font = FONT(14);
    _discountLabel.adjustsFontSizeToFitWidth = YES;
    [_bgView addSubview:_discountLabel];
}

- (void)setModel:(ByModelShopsListItem *)model {
    
    _model = model;
    
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    CGFloat margin = 10;
    CGFloat x, y, width, height;
    
    x = 0;
    y = 0;
    width = SCREEN_WIDTH - 20;
    height = self.height;
    _bgView.layer.cornerRadius = 10;
    _bgView.frame = CGRectMake(x, y, width, height);
    
    x = margin;
    y = margin;
    height = self.height - y * 2;
    width = height;
    _iconView.layer.cornerRadius = 4;
    _iconView.frame = CGRectMake(x, y, width, height);
    
    x = _iconView.right + margin;
    y = 0;
//    width = _bgView.width - 10 - x;
    width = 0;
    height = 0;
    _nameLabel.frame = CGRectMake(x, y, width, height);
    
    x = _nameLabel.right + margin;
    y = 0;
    width = 0;
    height = 0;
    _discountLabel.frame = CGRectMake(x, y, width, height);
    
    x = _nameLabel.left;
    y = 0;
    width = 16 * 5;
    height = 16;
    [_starView setFrameOverride:CGRectMake(x, y, width, height)];
    
    x = _starView.right + margin;
    y = 0;
    width = _bgView.width - 10 - x;
    height = 15;
    _sellLabel.frame = CGRectMake(x, y, width, height);

    x = _nameLabel.left;
    y = 0;
    width = 20;
    height = 15;
    _moneyLabel.frame = CGRectMake(x, y, width, height);
    
    x = _moneyLabel.left;
    y = 0;
    width = 15;
    height = 15;
    _addressIcon.frame = CGRectMake(x, y, width, height);

    
    x = _addressIcon.left;
    y = 0;
    width = 20;
    height = 15;
    _distanceLabel.frame = CGRectMake(x, y, width, height);

    /*
     模拟数据
     */
    NSString *name = self.model.name;
    CGFloat starCount = self.model.grade.floatValue;
    NSString *sells = [NSString stringWithFormat:@"年售%@单", self.model.monthly_sales];
    NSString *money = [NSString stringWithFormat:@"%@/人", self.model.price];
    NSString *distance = [NSString stringWithFormat:@"距我%@米", self.model.distance];
    NSString *discount = [NSString stringWithFormat:@"%.1lf折",[self.model.discount_rate floatValue] * 10];
//    CGFloat nameWidth = [name getSizeOfString:14].width + 20;
    CGFloat dis_width = [sells getSizeOfString:16].width + 10;
    CGFloat nameHeight = MIN([name getSizeOfString:16].height, 35);
    CGFloat remain = (_bgView.height - nameHeight - _starView.height - _moneyLabel.height - margin * 2) / 2;
    
    _iconView.backgroundColor = [UIColor redColor];
    [_iconView setImageWithURL:[NSURL URLWithString:self.model.main_img_small_urlstr] placeholderImage:DEFAULT_IMAGE completed:nil];
    
    _nameLabel.numberOfLines = 1;
    _nameLabel.height = nameHeight;
    _nameLabel.width = _bgView.width - _iconView.width - 40 - dis_width;
    _nameLabel.top = remain;
    _nameLabel.text = name;
    
    _discountLabel.left = _nameLabel.right + margin;
    _discountLabel.top = remain;
    _discountLabel.height = nameHeight;
    _discountLabel.width = dis_width;
    _discountLabel.text = sells ;
    
    _starView.top = _nameLabel.bottom + margin;
    [_starView setRatio:starCount / 5.0];
    
    _sellLabel.y = _starView.y;
    _sellLabel.left = _starView.right + 10;
    _sellLabel.text = discount;
    
    _moneyLabel.top = _sellLabel.bottom + margin;
    _moneyLabel.width = [money getSizeOfString:14].width + 5;
    _moneyLabel.text = money;
    
    _addressIcon.left = _moneyLabel.right + 5;
    _addressIcon.top = _moneyLabel.top;
    
    _distanceLabel.hidden = !self.showDistance;
    _distanceLabel.left = _addressIcon.right;
    _distanceLabel.top = _addressIcon.top;
    _distanceLabel.width = [distance getSizeOfString:14].width + 5;
    _distanceLabel.width = MIN(_distanceLabel.width, _bgView.width - 5 - _distanceLabel.left);
    _distanceLabel.text = distance;
}
@end
